﻿using System;
using System.Web.UI.WebControls;
using EOSCRM.Dao;
using EOSCRM.Domain;
using EOSCRM.Util;
using EOSCRM.Web.Common;
using EOSCRM.Web.Shared;
using System.Collections.Generic;

namespace EOSCRM.Web.Forms.DongHo
{
    public partial class NhapDongHoKhachHang : Authentication
    {
        private readonly KhachHangDao khDao = new KhachHangDao();
        private readonly DuongPhoDao dpDao = new DuongPhoDao();
        private readonly LoaiDongHoDao ldhDao = new LoaiDongHoDao();
        private readonly KhuVucDao kvDao = new KhuVucDao();
        private readonly PhuongDao pDao = new PhuongDao();

        #region Properties
        

        protected String IDKH
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_IDKH) ?
                    null :
                    EncryptUtil.Decrypt(param[Constants.PARAM_IDKH].ToString());
            }
        }

        protected String SOHD
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_SOHD) ?
                    null :
                    EncryptUtil.Decrypt(param[Constants.PARAM_SOHD].ToString());
            }
        }

        protected String MADH
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_MADH)
                           ? null
                           : EncryptUtil.Decrypt(param[Constants.PARAM_MADH].ToString());
            }
        }

        protected String TENKH
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_TENKH)
                           ? null
                           : EncryptUtil.Decrypt(param[Constants.PARAM_TENKH].ToString());
            }
        }

        protected String SONHA
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_SONHA)
                           ? null
                           : EncryptUtil.Decrypt(param[Constants.PARAM_SONHA].ToString());
            }
        }

        protected String TENDP
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                return !param.ContainsKey(Constants.PARAM_TENDP)
                           ? null
                           : EncryptUtil.Decrypt(param[Constants.PARAM_TENDP].ToString());
            }
        }

        protected String MAKV
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                if (!param.ContainsKey(Constants.PARAM_AREACODE))
                    return null;

                var mkv = EncryptUtil.Decrypt(param[Constants.PARAM_AREACODE].ToString());
                var kv = kvDao.Get(mkv);
                return kv == null ? null : mkv;
            }
        }

        protected String MAPHUONG
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                if (!param.ContainsKey(Constants.PARAM_STATECODE))
                    return null;
                var mp = EncryptUtil.Decrypt(param[Constants.PARAM_STATECODE].ToString());
                var p = pDao.Get(mp);
                return p == null ? null : mp;
            }
        }
        protected String LOAIKHACHHANG
        {
            get
            {
                var param = ParameterWrapper.GetParams();
                if (!param.ContainsKey(Constants.PARAM_KHACHHANGTYPE))
                    return null;
                var mp = EncryptUtil.Decrypt(param[Constants.PARAM_KHACHHANGTYPE].ToString());
                return mp;
            }
        }
        protected bool HasFiltered
        {
            get
            {
                return ParameterWrapper.GetParams().Keys.Count > 0;
            }
        }
        #endregion
        private List<LOAIDH> ListLoaiDH
        {
            get
            {
                if (Session["LoaiDH"] != null)
                    return Session["LoaiDH"] as List<LOAIDH>;
                return ldhDao.GetList();
            }
        }
        private bool IsDataValid()
        {
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxCRM), Page);
                var listldh = ldhDao.GetList();
                Session["LoaiDH"] = listldh;
                if (!Page.IsPostBack)
                {
                    if (HasFiltered)
                    {
                        BindKhachHangGrid();
                        BindFilterPanel();
                    }
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        #region Startup script registeration
        private void SetControlValue(string id, string value)
        {
            ((EOS)Page.Master).SetControlValue(id, value);
        }

        private void SetLabel(string id, string value)
        {
            ((EOS)Page.Master).SetLabel(id, value);
        }

        private void ShowError(string message, string controlId)
        {
            ((EOS)Page.Master).ShowError(message, controlId);
        }

        private void ShowError(string message)
        {
            ((EOS)Page.Master).ShowError(message);
        }

        private void ShowInfor(string message)
        {
            ((EOS)Page.Master).ShowInfor(message);
        }

        private void ShowWarning(string message)
        {
            ((EOS)Page.Master).ShowWarning(message);
        }

        private void HideDialog(string divId)
        {
            ((EOS)Page.Master).HideDialog(divId);
        }

        private void UnblockDialog(string divId)
        {
            ((EOS)Page.Master).UnblockDialog(divId);
        }

        private void UnblockWaitingDialog()
        {
            ((EOS)Page.Master).UnblockWaitingDialog();
        }

        private void CloseWaitingDialog()
        {
            ((EOS)Page.Master).CloseWaitingDialog();
        }
        #endregion

        private void BindFilterPanel()
        {
            filterPanel.AreaCode = MAKV;
            filterPanel.IDKH = IDKH;
            filterPanel.SOHD = SOHD;
            filterPanel.MADH = MADH;
            filterPanel.TENKH = TENKH;
            filterPanel.SONHA = SONHA;
            filterPanel.TENDP = TENDP;
            filterPanel.Statecode = MAPHUONG;
            filterPanel.Loaikhachhang = LOAIKHACHHANG;
        }

        
        


        #region Control event handlers
        

       

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!HasUpdatePermission)
                {
                    CloseWaitingDialog();
                    ShowError(Resources.Message.WARN_PERMISSION_DENIED);
                    return;
                }

                //var kh = KhachHang;

                //if (kh == null)
                //{
                //    CloseWaitingDialog();
                //    return;
                //}
                
                ////kh.IDKH = hdfIDKH.Value;

                //var msg = khDao.Update(kh, DateTime.Now.Month, DateTime.Now.Year, CommonFunc.GetComputerName(), CommonFunc.GetIpAdddressComputerName(), LoginInfo.MANV);

                //if (!msg.MsgType.Equals(MessageType.Error))
                //{
                //    ClearForm();
                //    divCustomersContainer.Visible = false;
                //    // bind grid
                //    BindKhachHangGrid();
                //    CloseWaitingDialog();

                //    ShowInfor(ResourceLabel.Get(msg));
                //}
                //else
                //{
                //    CloseWaitingDialog();
                //    ShowError("<strong>Lỗi xảy ra</strong>. <br/><br/>Dò lỗi: <br />" +
                //        ResourceLabel.Get(msg), txtMADP.ClientID);
                //}
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        
        #endregion

        #region Khách hàng
        private void BindKhachHangGrid()
        {
            var list = khDao.GetList(IDKH, SOHD, MADH, TENKH, SONHA, TENDP, MAKV, MAPHUONG,PhuongQuanLy,LOAIKHACHHANG);
            gvKhachHang.DataSource = list;
            gvKhachHang.PagerInforText = list.Count.ToString();
            gvKhachHang.DataBind();
            

            upnlCustomers.Update();
        }

        #endregion

        protected void gvKhachHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
            var ddlMaLoaiDH = e.Row.FindControl("ddlMaLoaiDH") as DropDownList;
            if (ddlMaLoaiDH == null) return;
            CommonFunc.BindDropDownList(ddlMaLoaiDH, ListLoaiDH, "MALDH", "MALDH", false);
            var listkh=gvKhachHang.DataSource as List<KHACHHANG>;
            if(listkh==null)
                return;
            var makh = listkh[e.Row.RowIndex + gvKhachHang.PageIndex * gvKhachHang.PageSize].MADP + listkh[e.Row.RowIndex + gvKhachHang.PageIndex * gvKhachHang.PageSize].DUONGPHU + listkh[e.Row.RowIndex + gvKhachHang.PageIndex * gvKhachHang.PageSize].MADB;
            var kh = khDao.GetKhachHangFromMadb(makh);
            if (kh != null)
            {
                var ldh = kh.MALDH;
                if (ldh != null)
                {
                    var _dh = ddlMaLoaiDH.Items.FindByValue(ldh);
                    if (_dh != null)
                    {
                        ddlMaLoaiDH.SelectedIndex = ddlMaLoaiDH.Items.IndexOf(_dh);
                    }
                }
            }
            var txtmadh = e.Row.FindControl("txtMaDH") as TextBox;
            var txtngaythay = e.Row.FindControl("txtNgayThay") as TextBox;
            var ddlmldh = e.Row.FindControl("ddlMaLoaiDH") as DropDownList;
            var txtngayhethan = e.Row.FindControl("txtNgayHetHan") as TextBox;
            if (string.IsNullOrEmpty(kh.MADH))
                txtmadh.Text = "";
            if (!kh.NGAYTHAYDH.HasValue)
                txtngaythay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            else
            {
                txtngaythay.Text = kh.NGAYTHAYDH.Value.ToString("dd/MM/yyyy");
            }
            if (!kh.NGAYHETHANDH.HasValue)
            {
                txtngayhethan.Text = DateTime.Now.AddYears(5).ToString("dd/MM/yyyy");
            }
            else
            {
                txtngayhethan.Text = kh.NGAYHETHANDH.Value.ToString("dd/MM/yyyy");
            }
            if (txtmadh==null || txtngaythay==null || txtngayhethan==null || ddlmldh==null) return;
            string madon = listkh[e.Row.RowIndex + gvKhachHang.PageIndex * gvKhachHang.PageSize].IDKH;
            string manv=LoginInfo.MANV;
            var script = "javascript:updateThayDongHo(\"" + madon + "\", \"" + txtngaythay.ClientID+
                                                                "\", \"" + txtngayhethan.ClientID +
                                                                "\", \"" + txtmadh.ClientID +
                                                                "\", \"" + ddlmldh.ClientID +
                                                                "\", \"" + manv+ 
                                                                "\")";
            txtngaythay.Attributes.Add("onchange", script);
            txtngayhethan.Attributes.Add("onchange", script);
            txtmadh.Attributes.Add("onchange", script);
            ddlmldh.Attributes.Add("onchange", script);
        }

        protected void btnSave_Click1(object sender, EventArgs e)
        {
            var source = gvKhachHang.DataSource as List<KHACHHANG>;

        }
    }
}